package edu.xpu.compute.recommend.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;

import edu.xpu.compute.recommend.vo.LoginUserVo;

public class LoginInterceptor implements HandlerInterceptor {
	
	private static final String USER_REGISTER_URL = "/recommend/register";
	private static final String FACULTY_ALL_URL = "/recommend//restful/meta/faculty/findAll";
	private static final String API_REGISTER_URL = "/recommend//restful/register";

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String requestURI = request.getRequestURI();
		if (requestURI.endsWith(USER_REGISTER_URL) || requestURI.endsWith(FACULTY_ALL_URL)
				|| requestURI.endsWith(API_REGISTER_URL)) {
			return true;
		}
		/*
		 * 获取登录用户, 如果没有登录,则拦截请求
		 */
		HttpSession session = request.getSession();
		LoginUserVo loginUser = (LoginUserVo) session.getAttribute("SESSION_USER");
		if (loginUser != null) {
			return true;
		}
		/*
		 * 没有登录成功, 返回登录页进行登录
		 */
		response.sendRedirect(request.getContextPath());
		return false;
	}
}
